<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('dishes', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('菜品名称');
            $table->text('description')->nullable()->comment('菜品描述');
            $table->unsignedBigInteger('category_id')->nullable()->comment('分类ID');
            $table->unsignedBigInteger('creator_id')->nullable()->comment('创建者ID');
            $table->unsignedBigInteger('menu_id')->nullable()->comment('菜单ID');
            $table->unsignedBigInteger('source_dish_id')->nullable()->comment('来源菜品ID（基于系统菜品创建）');
            $table->enum('dish_source', ['system', 'user', 'menu'])->default('system')->comment('菜品来源');
            $table->boolean('is_public')->default(false)->comment('是否公开（可被其他房间使用）');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
            $table->foreign('creator_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
            $table->foreign('source_dish_id')->references('id')->on('dishes')->onDelete('set null');
            $table->string('image')->nullable()->comment('图片URL');
            $table->json('tags')->nullable()->comment('标签数组');
            $table->json('flavors')->nullable()->comment('口味选项');
            $table->integer('cooking_time')->nullable()->comment('制作时间（分钟）');
            $table->boolean('is_available')->default(true)->comment('是否可用');
            $table->boolean('is_featured')->default(false)->comment('是否推荐');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->integer('view_count')->default(0)->comment('浏览次数');
            $table->integer('order_count')->default(0)->comment('点餐次数');
            $table->timestamps();
            
            $table->index('category_id');
            $table->index('creator_id');
            $table->index('menu_id');
            $table->index('source_dish_id');
            $table->index('dish_source');
            $table->index('is_public');
            $table->index('is_available');
            $table->index('is_featured');
            $table->index('sort_order');
            $table->index('name');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('dishes');
    }
};